package com.czxy.Service;

import com.czxy.Servlet.UserServlet;
import com.czxy.dao.UserDao;
import com.czxy.domain.User;

import java.util.List;
import java.util.UUID;

/**
 * @version v 1.0
 * @Author kami
 * @Date 2019/4/28
 */
public class UserService {
    private UserDao ud = new UserDao();


    public User login(String loginName, String password) {
        User userByUsername = ud.findUserByUsername(loginName);
        if (userByUsername!=null){
            if (userByUsername.getPassword().equals(password)){
                return userByUsername;
            }
        }
        return null;
    }

    public User insert(User user, String pwd){
        if (!user.getPassword().equals(pwd)) {
            UserServlet.errorRegister="确认密码与密码不同!注册失败!ᕦ(･ㅂ･)ᕤ";
            return null;
        }
        List<User> list = ud.findAll();  // 从数据库中获取所有数据
        boolean falg = true;
        for (User user1 : list) {
            System.out.println(user1);
        }
        for (User u : list) {    //循环判断LoginName是否相同
            if (u.getLoginName().equals(user.getLoginName())) {
                UserServlet.errorRegister="登录名已存在!注册失败!ᕦ(･ㅂ･)ᕤ";
                falg = false;
            }
            if (u.getPhone().equals(user.getPhone())){
                UserServlet.errorRegister="电话号码已存在!注册失败!ᕦ(･ㅂ･)ᕤ";
                falg = false;
            }
            if (u.getEmail().equals(user.getEmail())){
                UserServlet.errorRegister="邮箱已存在!注册失败!ᕦ(･ㅂ･)ᕤ";
                falg = false;
            }
        }
        if (falg) {
            String s = UUID.randomUUID().toString();
            user.setUid(s);
            boolean b = ud.insert(user);
            if (b) {
                return user;
            }
            return null;
        }
        return null;
    }
}
